OPC Studio User's Guide and Reference
WriteAllBytes(IWritableFileInfo,Byte[]) Method
Example 



OpcLabs.BaseLib Assembly > OpcLabs.BaseLib.Extensions.FileProviders Namespace > IWritableFileInfoExtension Class > WriteAllBytes Method : WriteAllBytes(IWritableFileInfo,Byte[]) Method
The writable file info object that will perform the operation.
Data to be written to the file.
Writes binary data to a file in an abstract writable file provider.
Syntax
'Declaration
 
<ExtensionAttribute()>
Public Overloads Shared Sub WriteAllBytes( _
   ByVal writableFileInfo As IWritableFileInfo, _
   ByVal data() As Byte _
) 
'Usage
 
Dim writableFileInfo As IWritableFileInfo
Dim data() As Byte
 
IWritableFileInfoExtension.WriteAllBytes(writableFileInfo, data)
[Extension()]
public static void WriteAllBytes( 
   IWritableFileInfo writableFileInfo,
   byte[] data
)
[Extension()]
public:
static void WriteAllBytes( 
   IWritableFileInfo^ writableFileInfo,
   array<byte>^ data
) 

Parameters

writableFileInfo
The writable file info object that will perform the operation.
data
Data to be written to the file.
Exceptions
ExceptionDescription

A null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.

This is a usage error, i.e. it will never occur (the exception will not be thrown) in a correctly written program. Your code should not catch this exception.

An I/O error has occurred.

This is an operation error that depends on factors external to your program, and thus cannot be always avoided. Your code must handle it appropriately.

An invoked method is not supported at all, or is not supported with the parameters used to create the object.

A security error was detected.

This is an operation error that depends on factors external to your program, and thus cannot be always avoided. Your code must handle it appropriately.

The operating system has denied access because of an I/O error or a specific type of security error.

This is an operation error that depends on factors external to your program, and thus cannot be always avoided. Your code must handle it appropriately.

Remarks

If the file does not already exist, the method creates it.

This method is similar to FileSystem.WriteAllBytes Method for normal (operating system) files.

Note: This method opens a file, writes to it, and then closes it. Code that uses this method is simpler than code that uses a System.IO.BinaryWriter object with a System.IO.Stream. However, if you are adding data to a file using a loop, a System.IO.BinaryWriter object can provide better performance because you only have to open and close the file once.

Example

.NET

// Shows how to write the full contents of an OPC UA file at once, using the file provider model.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

using System;
using System.Text;
using OpcLabs.BaseLib.Extensions.FileProviders;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.FileTransfer;

namespace UADocExamples.FileProviders._WritableFileInfo
{
    class WriteAllBytes
    {
        public static void Main1()
        {
            // Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            UAEndpointDescriptor endpointDescriptor = "opc.tcp://localhost:48030";

            // A node that represents an instance of OPC UA FileType object.
            UANodeDescriptor fileNodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile";
            
            // Instantiate the file transfer client object
            var fileTransferClient = new EasyUAFileTransferClient();

            Console.WriteLine("Getting writable file info...");
            IWritableFileInfo writableFileInfo = fileTransferClient.GetWritableFileInfo(endpointDescriptor, fileNodeDescriptor);
            // From this point onwards, the code is independent of the concrete realization of the file provider, and would
            // be identical e.g. for files in the physical file system, if the corresponding file provider was used.

            // Write all contents into a specified file node.
            byte[] bytes = Encoding.UTF8.GetBytes("TEXT FROM FILE TRANSFER CLIENT EXAMPLE. Demonstrates writing the whole contents of a file at once.");
            try
            {
                Console.WriteLine("Writing the whole file...");
                writableFileInfo.WriteAllBytes(bytes);

                // Due to an issue in the server, the file might not be readable now, without server restart.
                Console.WriteLine("Reading the data back...");
                byte[] data = writableFileInfo.ReadAllBytes();
                Console.WriteLine(Encoding.UTF8.GetString(data));
            }
            // Methods in the file provider model throw IOException and other exceptions, but not UAException.
            catch (Exception exception)
            {
                Console.WriteLine($"*** Failure: {exception.GetBaseException().Message}");
                return;
            }

            Console.WriteLine();
            Console.WriteLine("Finished...");
        }
    }
}
# Shows how to write the full contents of an OPC UA file at once, using the file provider model.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc

# Import .NET namespaces.
from System import *
from System.Text import *
from OpcLabs.BaseLib.Extensions.FileProviders import *
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.FileTransfer import *
from OpcLabs.EasyOpc.UA.Navigation import *


# Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe).
endpointDescriptor = UAEndpointDescriptor('opc.tcp://localhost:48030')

# A node that represents an OPC UA file system (a root directory).
fileNodeDescriptor = UANodeDescriptor('nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile')

# Instantiate the file transfer client object.
fileTransferClient = EasyUAFileTransferClient()

print('Getting writable file info...')
writableFileInfo = IEasyUAFileTransferExtension.GetWritableFileInfo(fileTransferClient,
                                                                    endpointDescriptor,
                                                                    UANamedNodeDescriptor(fileNodeDescriptor))
# From this point onwards, the code is independent of the concrete realization of the file provider, and would
# be identical e.g. for files in the physical file system, if the corresponding file provider was used.

# Write all contents into a specified file node.
bytes = Encoding.UTF8.GetBytes('TEXT FROM FILE TRANSFER CLIENT EXAMPLE. Demonstrates writing the whole contents of a '
                               'file at once.')
try:
    print('Writing the whole file...')
    IWritableFileInfoExtension.WriteAllBytes(writableFileInfo, bytes)

    # Due to an issue in the server, the file might not be readable now, without server restart.
    print('Reading the data back...')
    data = IFileInfoExtension.ReadAllBytes(writableFileInfo)
    print(Encoding.UTF8.GetString(data))

# Methods in the file provider model throw IOException and other exceptions, but not UAException.
except Exception as exception:
    print('*** Failure: ' + exception.GetBaseException().Message)
    exit()

print()
print('Finished.')
' Shows how to write the full contents of an OPC UA file at once, using the file provider model.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

Imports System.Text
Imports OpcLabs.BaseLib.Extensions.FileProviders
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.FileTransfer

Namespace FileProviders._WritableFileInfo

    Friend Class WriteAllBytes

        Public Shared Sub Main1()

            ' Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            Dim endpointDescriptor As UAEndpointDescriptor = "opc.tcp://localhost:48030"

            ' A node that represents an instance of OPC UA FileType object.
            Dim fileNodeDescriptor As UANodeDescriptor =
                "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile"

            ' Instantiate the file transfer client object
            Dim fileTransferClient = New EasyUAFileTransferClient

            Console.WriteLine("Getting writable file info...")
            Dim writableFileInfo As IWritableFileInfo =
                fileTransferClient.GetWritableFileInfo(endpointDescriptor, fileNodeDescriptor)
            ' From this point onwards, the code is independent of the concrete realization of the file provider, and would
            ' be identical e.g. for files in the physical file system, if the corresponding file provider was used.

            ' Write all contents into a specified file node.
            Dim bytes As Byte() = Encoding.UTF8.GetBytes("TEXT FROM FILE TRANSFER CLIENT EXAMPLE. Demonstrates writing the whole contents of a file at once.")
            Try
                Console.WriteLine("Writing the whole file...")
                writableFileInfo.WriteAllBytes(bytes)

                ' Due to an issue in the server, the file might Not be readable now, without server restart.
                Console.WriteLine("Reading the data back...")
                Dim data As Byte() = writableFileInfo.ReadAllBytes()
                Console.WriteLine(Encoding.UTF8.GetString(data))

                ' Methods in the file provider model throw IOException and other exceptions, but not UAException.
            Catch exception As Exception
                Console.WriteLine("*** Failure: {0}", exception.GetBaseException.Message)
                Exit Sub
            End Try

            Console.WriteLine()
            Console.WriteLine("Finished...")
        End Sub
    End Class
End Namespace
Requirements

Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows

See Also